@echo off
cd /d "%~dp0"
set ShowHelp=0
set SIGN_CMD="%~dp0\sign_tool\windows\atb_signer.exe"
set SIGNER_KEY="%~dp0\sign_tool\keys\TestRSA2048_ossl.pem"
@REM Obtain input file path
for %%P in ("%~1") do set "FilePath=%%~dpP"
@REM Obtain inpit file name
for %%F in ("%~1") do set "FileName=%%~nF"

echo FilePath=%FilePath%
echo FileName=%FileName%

if "%1" == "" (set ShowHelp=1 
goto PrintHelp
)

if "%2" == "" (set ShowHelp=1 
goto PrintHelp
)

set SIGNED_IMAGE=%1.rsa2048.signed

if exist %SIGNED_IMAGE% del %SIGNED_IMAGE%

@REM dlp = 0x08000000 + offset * 0x200, to/entry address means entryAddress
set core_list=--iib core=0 type=0x0 image=%1 dlp=%2 to=%3  entry=%4 --psn 0x01
@REM set core_list=--iib core=0 type=0x0 image=%1 dlp=0x208 to=0x08041000  entry=0x08041000 --psn 0x01
echo %core_list%  ccc
%SIGN_CMD% sign --v 2 --sec_ver 0x1234 --dgst sha256 --rcp key=%SIGNER_KEY% %core_list% --of %SIGNED_IMAGE%

@REM nohash signer for XIP debug
@REM set core_list= --iib nohash core=0 type=0x0 image=%1% dlp=0x208 to=%2%  entry=%3% --psn 0x01
@REM %SIGN_CMD% sign --v 2 --sec_ver 0x1234 --dgst sha256 --rcp key=%SIGNER_KEY% %core_list% --of %SIGNED_IMAGE%

@REM convert bin to hex
.\bin2hex\Bin2Hex.exe -b 0x08000000,%SIGNED_IMAGE% -o %FilePath%%FileName%.hex

@REM Bin2Hex.exe -b 0x10140000,test.bin,0x10340000,test.bin -o out.hex

@REM package
pactool\pactool make_pac_image_no_gpt --output %FilePath%%FileName%.pac ^
--allow_empty_partitions ^
--product E3650 ^
--da FDA:pactool/temp/nvm/FDA.bin ^
--image 0x8000000:%SIGNED_IMAGE%
@REM --image 0x8400000:temp/nvm/0x8000000.bin ^
@REM --image 0x8800000:temp/nvm/0x8000000.bin ^
@REM --image 0x8c00000:temp/nvm/0x8000000.bin ^
@REM --image 0x9000000:temp/nvm/0x8000000.bin 

:PrintHelp
if %ShowHelp% == 1 (echo;
echo Usage: %0 unsigned.bin dlpoffset entryAddress 
echo e.g.   Take e3 board for example, the command is as followed
echo        %0 .\sf.bin dlp entryAddress
echo;
)
